package com.zsg.service.impl;

import com.zsg.dao.UserMapper;
import com.zsg.pojo.User;
import com.zsg.service.UserService;
import com.zsg.utils.Constant;
import com.zsg.utils.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import sun.security.provider.MD5;
import sun.security.rsa.RSASignature;

import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private ResponseData responseData;

    @Autowired(required = false)
    private UserMapper userMapper;


    @Override
    public ResponseData addUser(User user) {
        ResponseData responseData = new ResponseData();
        //密码加密
        String newPassword = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        log.info("密码："+newPassword);
        user.setPassword(newPassword);

        int re = userMapper.addUser(user);

        if (re>0){
            responseData.setCode(Constant.SUCCESS);
            responseData.setMsg("添加成功");
            responseData.setData(null);
            return responseData;
        }
        responseData.setCode(Constant.FILED);
        responseData.setMsg("服务器错误");
        responseData.setData(null);
        return responseData;

    }

    @Override
    public ResponseData delUser(User user) {
        ResponseData responseData = new ResponseData();
        int re = userMapper.delUser(user);
        if (re>0){
            responseData.setCode(Constant.SUCCESS);
            responseData.setMsg("删除成功");
            responseData.setData(null);
            return responseData;
        }
        responseData.setCode(Constant.FILED);
        responseData.setMsg("服务器错误");
        responseData.setData(null);
        return responseData;
    }

    @Override
    public ResponseData updateUser(User user) {
        ResponseData responseData = new ResponseData();

        int re = userMapper.update(user);


        if (user.getId() ==null){
            responseData.setCode(Constant.FILED);
            responseData.setMsg("用户ID不能为空");
            responseData.setData(null);
            return responseData;
        }

        if (user.getPassword() !=null){
            user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        }

        if (re>0){
            responseData.setCode(Constant.SUCCESS);
            responseData.setMsg("修改成功");
            responseData.setData(null);
            return responseData;
        }
        responseData.setCode(Constant.FILED);
        responseData.setMsg("服务器错误");
        responseData.setData(null);
        return responseData;
    }


    /**
     * 查询用户
     * @param user
     * @return
     */
    @Override
    public ResponseData queryUser(User user) {
        ResponseData responseData = new ResponseData();
        List<User> users = userMapper.queryUser(user);
        responseData.setCode(Constant.SUCCESS);
        responseData.setMsg("查询成功");
        responseData.setData(users);
        return responseData;
    }



}
